﻿using System;
using System.Data;
using System.Data.SqlClient;
using Common;

namespace DataAccess
{
    public class UserDao
    {
        private SqlConnection _sqlConnection;
        public UserDao()
        {
            _sqlConnection = SqlTools.GetConnection();
        }

        /// <summary>
        /// Athenticates the user.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public int Authenticate(string userName, string password)
        {
            try
            {
                string encryptedPassword = EncryptPassword.MD5Encrypt(password);
                SqlParameter[] parameters = new SqlParameter[2];
                parameters[0] = new SqlParameter("@Username", SqlDbType.Text) { Value = userName };
                parameters[1] = new SqlParameter("@Password", SqlDbType.Text) { Value = encryptedPassword };
                return (int) SqlHelper.ExecuteScalar(_sqlConnection, CommandType.Text, SqlQuery.CheckUser, parameters);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Adds the user.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <param name="fullname">The fullname.</param>
        /// <param name="email">The email.</param>
        /// <param name="phone">The phone.</param>
        /// <param name="address">The address.</param>
        /// <param name="status">The status.</param>
        /// <returns></returns>
        public int AddUser(string userName, string password, string fullName, string email, string phone,
            string address, int status)
        {
            try
            {
                var parameters = new SqlParameter[7];
                parameters[0] = new SqlParameter("@Username", SqlDbType.Text) { Value = userName };
                parameters[1] = new SqlParameter("@Password", SqlDbType.Text) { Value = password };
                parameters[2] = new SqlParameter("@Fullname", SqlDbType.Text) { Value = fullName };
                parameters[3] = new SqlParameter("@Email", SqlDbType.Text) { Value = email };
                parameters[4] = new SqlParameter("@Phone", SqlDbType.Text) { Value = phone};
                parameters[5] = new SqlParameter("@Address", SqlDbType.Text) { Value = address };
                parameters[6] = new SqlParameter("@Status", SqlDbType.Bit) { Value = status };
                return SqlHelper.ExecuteNonQuery(_sqlConnection, CommandType.Text, SqlQuery.AddUser);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Updates the user.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="username">The username.</param>
        /// <param name="fullname">The fullname.</param>
        /// <param name="email">The email.</param>
        /// <param name="phone">The phone.</param>
        /// <param name="address">The address.</param>
        /// <param name="status">The status.</param>
        /// <returns></returns>
        public int UpdateUser(int userId, string userName, string fullName, string email, 
            string phone, string address, int status)
        {
            try
            {
                SqlParameter[] parameters = new SqlParameter[7];
                parameters[0] = new SqlParameter("@UserId", SqlDbType.Int) { Value = userId };
                parameters[1] = new SqlParameter("@Username", SqlDbType.Text) { Value = userName };
                parameters[2] = new SqlParameter("@Fullname", SqlDbType.Text) { Value = fullName };
                parameters[3] = new SqlParameter("@Email", SqlDbType.Text) { Value = email };
                parameters[4] = new SqlParameter("@Phone", SqlDbType.Text) { Value = phone };
                parameters[5] = new SqlParameter("@Address", SqlDbType.Text) {Value = address };
                parameters[6] = new SqlParameter("@Status", SqlDbType.Bit) { Value = status };
                return SqlHelper.ExecuteNonQuery(_sqlConnection, CommandType.Text, SqlQuery.UpdateUser, parameters);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Views all user.
        /// </summary>
        /// <returns></returns>
        public DataTable ViewAllUser()
        {
            try
            {
                DataSet dataSet = SqlHelper.ExecuteDataSet(_sqlConnection, CommandType.Text, SqlQuery.ViewAllUser);
                return dataSet.Tables[0];
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        /// <summary>
        /// Views the user by id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public DataTable ViewUserById(int userId)
        {
            try
            {
                var parameter = new SqlParameter("@UserId", SqlDbType.Int) {Value = userId};
                DataSet dataSet = SqlHelper.ExecuteDataSet(_sqlConnection, CommandType.Text, SqlQuery.ViewUserById,
                                                           parameter);
                return dataSet.Tables[0];
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// Deactives the user.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public int DeactivateUser(int userId)
        {
            try
            {
                var parameter = new SqlParameter("@UserId", SqlDbType.Int) {Value = userId};
                return SqlHelper.ExecuteNonQuery(_sqlConnection, CommandType.Text, SqlQuery.DeactivateUser, parameter);
            }
            catch (Exception)
            {
                
                throw;
            }
        }

    }
}
